//
//  ASCallNameMsgView.m
//  AttendenceSys
//
//  Created by ld on 16/9/19.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASCallNameMsgView.h"

@implementation ASCallNameMsgView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // initialising all default values
        _infoArray = [[NSMutableArray alloc] init];
        
        [self initView];
    }
    return self;
}

-(void)initView
{
    _msgTabView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _msgTabView.delegate = self;
    _msgTabView.dataSource = self;
    _msgTabView.backgroundColor = [UIColor clearColor];
    _msgTabView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    _msgTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _msgTabView.scrollEnabled = YES;
    [_msgTabView registerClass:[ASCallNameMsgCell class] forCellReuseIdentifier:NSStringFromClass([ASCallNameMsgCell class])];
    [self addSubview:_msgTabView];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ASNewMsgInfo* msgInfo = [_infoArray objectAtIndex:[indexPath row]];
    
    ASCallNameMsgCell* cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ASCallNameMsgCell class])];
    [cell resetCellStatus];
    //cell.delegate = self;
    msgInfo.courseName = msgInfo.courseName ? msgInfo.courseName : @"";
    msgInfo.actionDesc = msgInfo.actionDesc ? msgInfo.actionDesc : @"";
    msgInfo.leaveDate = msgInfo.leaveDate ? msgInfo.leaveDate : @"";
    if (5 < msgInfo.leaveDate.length) {
        msgInfo.leaveDate = [msgInfo.leaveDate substringFromIndex:5];
    }
    //NSString* course = [NSString stringWithFormat:@"第%@节课", msgInfo.courseNumber];
    NSString *couseInfo = [NSString stringWithFormat:@"%@ %@ %@ %@", msgInfo.leaveDate,msgInfo.courseName,msgInfo.actionDesc,msgInfo.userName];
    cell.appBegEndTimeLab.text = couseInfo;
    /*
    CGSize detailSize = [couseInfo sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(200, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
    
    CGRect frame = cell.reviewLab.frame;
    frame.origin.x = detailSize.width + 10;
    cell.reviewLab.frame = frame;
     */
    //cell.appTimeLab.text = [NSString stringWithFormat:@"申请时间: %@", msgInfo.submitDate];
    // 设置请假批复按钮状态 // approveCode 1审批通过 2审批不通过 3待审批 4已经撤销
    [self updateCellButtonStatus:msgInfo msgCell:cell];
    //cell.appInfoLab.text = [NSString stringWithFormat:@"第%@节课", msgInfo.courseNumber,cell.reviewLab.text];
    //cell.msgDesc.text = msgInfo.actionDesc;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_infoArray count];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return AsNewMsgCellHeight/2;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)updateCellButtonStatus:(ASNewMsgInfo *)msgInfo msgCell:(ASCallNameMsgCell *)cell
{
    // 设置请假批复按钮状态 // approveCode 1审批通过 2审批不通过 3待审批 4已经撤销
    if ([msgInfo.approveCode isEqualToString:@"1"])
    {
        cell.reviewLab.text = @"已同意";
    }
    else if ([msgInfo.approveCode isEqualToString:@"2"])
    {
        cell.reviewLab.text = @"不通过";
    }
    else if ([msgInfo.approveCode isEqualToString:@"3"])
    {
        cell.reviewLab.text = @"待审批";
    }
    else if ([msgInfo.approveCode isEqualToString:@"4"])
    {
        cell.reviewLab.text = @"已撤销";
    }
    
}

- (void)fetchTeacherNewMsg
{
    //消息接口 http://211.65.216.54/getStudentDutyList.action?userCode=xiaohu&page=1&rowNum=10&userType=1(1表示老师2表示学生)
    
    if (_infoArray == nil)
    {
        _infoArray = [[NSMutableArray alloc] init];
    }
    
    NSString *refer = @"getStudentDutyList.action";
    NSString *prefix = kASHttpUrl(refer);
    NSString *teacherCode = _userCode;
    //NSString *teacherCode = [ASAppDelegate sharedAppDelegate].userInfo.userCode;
    NSString *rowNum = @"10";
    NSString *page = @"1";
    NSString *totalUrl = [NSString stringWithFormat:@"%@?userCode=%@&page=%@&rowNum=%@&userType=1", prefix, teacherCode, page, rowNum];
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    
    typeof(self) __weak weakSelf = self;
    [networkingEngine POST:totalUrl parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            [weakSelf parseTeacherNewMsg:data];
            [SVProgressHUD dismiss];
            
            [_msgTabView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseTeacherNewMsg:(NSArray *)array
{
    for (unsigned int i = 0; i < [array count]; i++)
    {
        ASNewMsgInfo* msgInfo = [[ASNewMsgInfo alloc] init];
        
        msgInfo.actionDesc = [[array objectAtIndex:i] objectForKey:@"actionDesc"];
        msgInfo.actionPicture = [[array objectAtIndex:i] objectForKey:@"actionPicture"];
        msgInfo.courseNumber = [[array objectAtIndex:i] objectForKey:@"courseNumber"];
        msgInfo.imageUrl = [[array objectAtIndex:i] objectForKey:@"imageUrl"];
        msgInfo.leaveDate = [[array objectAtIndex:i] objectForKey:@"leaveDate"];
        msgInfo.pkApproHistoryId = [[array objectAtIndex:i] objectForKey:@"pkApproHistoryId"];
        msgInfo.portraitNum = [[array objectAtIndex:i] objectForKey:@"portraitNum"];
        msgInfo.submitDate = [[array objectAtIndex:i] objectForKey:@"submitDate"];
        msgInfo.userCode = [[array objectAtIndex:i] objectForKey:@"userCode"];
        msgInfo.userName = [[array objectAtIndex:i] objectForKey:@"userName"];
        msgInfo.weekDay = [[array objectAtIndex:i] objectForKey:@"weekDay"];
        msgInfo.approveCode = [[array objectAtIndex:i] objectForKey:@"approveCode"];
        msgInfo.courseId = [[array objectAtIndex:i] objectForKey:@"courseId"];
        msgInfo.courseName = [[array objectAtIndex:i] objectForKey:@"courseName"];
        
        [_infoArray addObject:msgInfo];
    }
}

@end
